package servlet;

import dao.TaiKhoanDao;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.TaiKhoanPoJo;

/**
 *
 * @author Nautilus
 */
public class DangNhapServlet extends HttpServlet 
{

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        String strUsername = request.getParameter("txtUsername");
	String strPassword = request.getParameter("txtPassword");
	TaiKhoanDao dao = new TaiKhoanDao();
	TaiKhoanPoJo user = dao.DangNhap(strUsername, strPassword);
		
	RequestDispatcher dispatcher = null;
	
		
	if(user == null)
	{
			
            dispatcher = request.getRequestDispatcher("/index.jsp?f=1");
            dispatcher.forward(request, response);
            return;
        }
		
	HttpSession session = request.getSession();
	session.setAttribute("loggedUser", user);
		
	String preUrl = (String)session.getAttribute("preUrl");
		
	if(preUrl == null)
		preUrl = "/index.jsp";
		
	dispatcher = request.getRequestDispatcher(preUrl);
	dispatcher.forward(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
